package cn.edu.gdut.csq.util;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.Alignment;
import jxl.write.Colour;
import jxl.write.Label;
import jxl.write.VerticalAlignment;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class ExportExcelUtil {
	public static void exportExcel(HttpServletResponse response) {
		try {
			OutputStream os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流

			response.setHeader("Content-disposition", "attachment; filename="
					+ new String("词性数据".getBytes("GB2312"), "8859_1") + ".xls");// 设定输出文件头
			response.setContentType("application/msexcel");// 定义输出类型

			WritableWorkbook wwb = Workbook.createWorkbook(os); // 建立excel文件

			WritableSheet ws = wwb.createSheet("Sheet1", 10); // 创建一个工作表

			// 设置单元格的文字格式
			WritableFont wf = new WritableFont(WritableFont.ARIAL, 10,
					WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
					Colour.BLUE);
			WritableCellFormat wcf = new WritableCellFormat(wf);
			wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
			wcf.setAlignment(Alignment.CENTRE);
			ws.setRowView(0, 500);

			// 填充数据的内容
			// 填充表头
			Label name = new Label(0, 0, "姓名");
			Label sex = new Label(1, 0, "性别");
			
			// 填充数据
			Label nameValue = new Label(0, 1, "tom");
			Label sexValue = new Label(1, 1, "man");
			
			
			ws.addCell(name);
			ws.addCell(sex);
			ws.addCell(nameValue);
			ws.addCell(sexValue);
			
			wwb.write();
			wwb.close();

		} catch (IOException e) {
			e.printStackTrace();
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}
	}
}